package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.Order;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private AddressService addressService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findByName")
    public Result findByName(String name){
        Address address = addressService.findByName(name);
        return new Result(true,"查询成功",address);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        System.out.println(address);
        try {
            addressService.add(address);
            return new Result(true,"体检机构地址保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"体检机构地址保存失败");
        }

    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //获取名字
        String originalFilename=imgFile.getOriginalFilename();
        //取最后一个点的位置
        int index = originalFilename.lastIndexOf(".");
        //对点前一位后的位置分割
        String extention = originalFilename.substring(index - 1);
        //把后缀 与UUID进行拼接
        String fileName= UUID.randomUUID().toString()+extention;
        try {
            //调用七牛云上传插件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //调用自定义类将文件名存储到大集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

        } catch (IOException e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

    }

    @RequestMapping("/findById")
    public Result edit(Integer id){
        try {
            Address address = addressService.findById(id);
            return new Result(true,"查询地址成功",address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询地址失败");
        }
    }

    @RequestMapping("/handleEdit")
    public Result handleEdit(@RequestBody Address address){

        try{
            addressService.edit(address);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, "修改失败");
        }
        return new Result(true,"修改成功");
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        List<Order> list = addressService.findOrderByAddressId(id);
        System.out.println(list);
        if (list == null) {
            return new Result(false, "删除失败,要删除的地址有预约安排："+list);
        }
        try {
            addressService.delete(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

}
